//
//  ViewController.m
//  网络获取图片
//
//  Created by Justsoso on 16/3/22.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;

@property (nonatomic,strong) NSArray *arr;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)btn1Pressed:(id)sender
{
    
    [self.activityView startAnimating];
    [self performSelectorInBackground:@selector(image3Info) withObject:nil];
    
}
- (IBAction)btn2Pressed:(id)sender
{
    [self.activityView startAnimating];
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    
    dispatch_async(queue, ^{
        for (int i=0; i<self.arr.count; i++)
        {
            // 网址对象
            NSURL *url = [NSURL URLWithString:self.arr[i]];
            // 获取数据
            NSData *data = [NSData dataWithContentsOfURL:url];
            // 图像对象
            UIImage *image = [UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                // 图像视图
                UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
                imgView.frame = CGRectMake((i%3)*100+20, (i/3)*100+200, 100, 100);
                // 添加到视图
                [self.view addSubview:imgView];
                
            });
        }
        [self.activityView stopAnimating];
    });
}
- (IBAction)btn3Pressed:(id)sender
{
    
    [self.activityView startAnimating];
    
    dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
        dispatch_group_t group = dispatch_group_create();
        
        for (int i=0; i<self.arr.count; i++)
        {
            
            // 网址对象
            NSURL *url = [NSURL URLWithString:self.arr[i]];
            
            dispatch_group_async(group, queue, ^{
                // 获取数据
                NSData *data = [NSData dataWithContentsOfURL:url];
                // 图像对象
                UIImage *image = [UIImage imageWithData:data];
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
                    imgView.frame = CGRectMake((i%3)*100+20, (i/3)*100+200, 100, 100);
                    [self.view addSubview:imgView];
                });
            });
        }
    
//        dispatch_group_notify(group, queue, ^{
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.activityView stopAnimating];
//            });
//        });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [self.activityView stopAnimating];
    });
    

}


- (void)image3Info
{
    for (int i=0; i<self.arr.count; i++)
    {
        // 网址对象
        NSURL *url = [NSURL URLWithString:self.arr[i]];
        // 获取数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 图像对象
        UIImage *image = [UIImage imageWithData:data];
        
        [self performSelectorOnMainThread:@selector(addImageViewWith:) withObject:@[image,@(i)] waitUntilDone:NO];
    }
    [self.activityView performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}

- (void)addImageViewWith:(NSArray *)arr
{
    UIImage *image = arr[0];
    int i = [arr[1] intValue];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake((i%3)*100+20, (i/3)*100+200, 100, 100);
    [self.view addSubview:imageView];
}

@end
